# 测试获取外置USB摄像头列表

import cv2


def list_ports():
    """Test the ports and returns a tuple with the available ports and the ones that are working."""
    is_working = True
    dev_port = 0
    working_ports = []
    available_ports = []
    while is_working:
        camera = cv2.VideoCapture(dev_port)
        if camera.isOpened():
            # working capture
            is_reading, img = camera.read()
            w = camera.get(3)
            h = camera.get(4)
            if is_reading:
                # working port
                print("Port %s is working and reads images (%s x %s)" % (dev_port, h, w))
                working_ports.append(dev_port)
            else:
                print("Port %s for camera ( %s x %s) is present but does not reads." % (dev_port, h, w))
                available_ports.append(dev_port)
        else:
            print("Port %s is not present" % dev_port)
        dev_port += 1
        if dev_port > 10:
            break
    return available_ports, working_ports


list_ports()
